//删除注释
export const skipComments = (str: string) => {
  let index = -1;
  let count = 0;
  const deleteStrs: string[] = [];
  const se = (t: string) => str.indexOf(t, index);
  while (1) {
    if (count > 10000) return "循环次数超过一万";
    count++;
    let action = { min: index, code: "" };
    const setAction = (str: string) => {
      if (
        se(str) !== -1 &&
        se(str) > index &&
        (action.min === index ? true : se(str) < action.min)
      )
        action = { min: se(str), code: str };
    };
    setAction('"');
    setAction("`");
    setAction("'");
    setAction("//");
    setAction("/*");

    let lastIndex = 0;
    switch (action.code) {
      case "`":
      case '"':
      case "'":
        index = str.indexOf(action.code, action.min + 1); //2023-3-8,删除索引+1
        break;
      case "//":
      case "/*":
        lastIndex = str.indexOf(
          action.code === "//" ? "\n" : "*/",
          action.min + (action.code === "//" ? 1 : 2)
        );

        const deleteStr = str.slice(
          action.min,
          lastIndex + (action.code == "/*" ? 2 : 0) //2023-3-8,新增如果是/*slice多往后匹配两个
        );
        deleteStrs.push(deleteStr);
        index = lastIndex;
        break;
      default:
        deleteStrs.forEach((s) => (str = str.replace(s, "")));
        return str;
    }
  }
};
